package com.example.sihookuiklydemo

import com.example.myapplication.base.NavBar
import com.example.sihookuiklydemo.base.BasePager
import com.example.sihookuiklydemo.ble.getNewFloatPx
import com.tencent.kuikly.core.annotations.Page
import com.tencent.kuikly.core.base.Color
import com.tencent.kuikly.core.base.ViewBuilder
import com.tencent.kuikly.core.base.attr.ImageUri
import com.tencent.kuikly.core.layout.FlexPositionType
import com.tencent.kuikly.core.log.KLog
import com.tencent.kuikly.core.module.RouterModule
import com.tencent.kuikly.core.module.toPlatformObject
import com.tencent.kuikly.core.nvi.serialization.json.JSONObject
import com.tencent.kuikly.core.reactive.handler.observable
import com.tencent.kuikly.core.views.Image
import com.tencent.kuikly.core.views.Text
import com.tencent.kuikly.core.views.View

@Page("DeviceInfo")
internal class DeviceInfoPage : BasePager() {
    var deviceInfo: JSONObject? by observable(null)
    override fun body(): ViewBuilder {
        val ctx = this
        return {
            NavBar {
                attr {
                    title = ctx.titleName ?: "关于设备"
                    rightIconShow = false
                    isDarkMode = ctx.isNightMode()
                }
                event {
                    onClick {
                        ctx.acquireModule<RouterModule>(RouterModule.MODULE_NAME).closePage()
                    }

                }
            }
            attr {
                backgroundColor(ctx.theme.colors.background)
            }
            Image {
                attr {
                    size(ctx.pagerData.pageViewWidth, 280f)
                    src(ImageUri.commonAssets("device_about_a.png"))
                    resizeContain()
                    absolutePosition(0f, 0f)

                }
            }
            Image {
                attr {
                    size(200f, 208f)
                    src(ImageUri.commonAssets("shebei_desk.png"))
                    top(48f)
                    alignSelfFlexEnd()
                    resizeCover()
                    right(22f)
                }
            }
            View {
                attr {
                    marginTop(16f)
                    backgroundColor(ctx.theme.colors.grey_2)
                    height(1f)
                }
            }
            View {
                attr {
                    height(60f)
                    flexDirectionRow()
                    alignItemsCenter()
                }
                Text {
                    attr {
                        flex(1f)
                        left(24f)
                        fontSize(getNewFloatPx(14f))
                        text(ctx.getLanguage("device_device_model","设备型号"))
                        color(ctx.theme.colors.mainTextColor)
                        useDpFontSizeDim(true)
                    }

                }
                Text {
                    attr {
                        useDpFontSizeDim(true)
                        right(24f)
                        flex(1f)
                        textAlignRight()
                        color(ctx.theme.colors.mainTextColor)
                        fontSize(getNewFloatPx(14f))
                        text(ctx.deviceInfo?.optString("name") ?: "D07智能电动桌")
                    }

                }
            }
            View {
                attr {
                    backgroundColor(ctx.theme.colors.grey_2)
                    height(1f)
                }
            }
            View {
                attr {
                    height(60f)
                    flexDirectionRow()
                    alignItemsCenter()
                }
                Text {
                    attr {
                        flex(1f)
                        left(24f)
                        fontSize(getNewFloatPx(14f))
                        text(ctx.getLanguage("device_ble_name","蓝牙名"))
                        color(ctx.theme.colors.mainTextColor)
                        useDpFontSizeDim(true)
                    }

                }
                Text {
                    attr {
                        right(24f)
                        useDpFontSizeDim(true)
                        flex(1f)
                        fontSize(getNewFloatPx(14f))
                        textAlignRight()
                        color(ctx.theme.colors.mainTextColor)
                        text(ctx.deviceInfo?.optString("btBleName") ?: "SIHOO D07 DESK-0078")
                    }

                }
            }
            View {
                attr {
                    backgroundColor(ctx.theme.colors.grey_2)
                    height(1f)
                }
            }
            View {
                attr {
                    height(60f)
                    flexDirectionRow()
                    alignItemsCenter()
                }
                Text {
                    attr {
                        flex(1f)
                        left(24f)
                        fontSize(getNewFloatPx(14f))

                        text(ctx.getLanguage("device_macaddress","Mac地址"))
                        color(ctx.theme.colors.mainTextColor)
                        useDpFontSizeDim(true)
                    }

                }
                Text {
                    attr {
                        right(24f)
                        flex(1f)
                        fontSize(getNewFloatPx(14f))
                        textAlignRight()
                        color(ctx.theme.colors.mainTextColor)
                        useDpFontSizeDim(true)
                        text(ctx.deviceInfo?.optString("macAddress") ?: "A6:C0:80:83:00:78")
                    }

                }
            }
            View {
                attr {
                    backgroundColor(ctx.theme.colors.grey_2)
                    height(1f)
                }
            }
            View {
                attr {
                    height(60f)
                    flexDirectionRow()
                    alignItemsCenter()
                }
                Text {
                    attr {
                        flex(1f)
                        left(24f)
                        fontSize(getNewFloatPx(14f))
                       // text("固件版本")
                        text(ctx.getLanguage("device_firmware_version","固件版本"))

                        color(ctx.theme.colors.mainTextColor)
                        useDpFontSizeDim(true)
                    }

                }
                Text {
                    attr {
                        right(24f)
                        flex(1f)
                        textAlignRight()
                        fontSize(getNewFloatPx(14f))
                        text("--")
                        color(ctx.theme.colors.mainTextColor)
                        useDpFontSizeDim(true)
                    }

                }
            }
            View {
                attr {
                    backgroundColor(ctx.theme.colors.grey_2)
                    height(1f)
                }
            }
        }
    }

    override fun created() {
        super.created()
        toPlatformObject()
        showDeviceInfo()
    }

    fun showDeviceInfo() {
        var pageJson = pagerData.params
        KLog.i("deviceinfo", "$pageJson")
        pageJson?.let {
            var token = it.optString("token")
            var userId = it.optInt("userId")
            var eqId = it.optInt("equipmentId")

            deviceInfo = it?.optJSONObject("bindDeviceInfo")
            var deviceString = it?.optString("bindDeviceInfo")
            KLog.i("deviceinfoString", "$deviceString")
            deviceInfo?.let {
                KLog.i("deviceinfoJson", "$it")
            }
        }
    }
}